package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exp.GuiguException;
import com.atguigu.spzx.manager.mapper.CategoryBrandMapper;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.dto.product.CategoryBrandDto;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.CategoryBrand;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class categoryBrandServiceImpl implements CategoryBrandService {

    //分页三大步
    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, CategoryBrandDto categoryBrandDto) {
       //1.设置分页参数
        PageHelper.startPage(pageNum,pageSize);

        //2.执行sql
      List<CategoryBrand> list =  categoryBrandMapper.selectList(categoryBrandDto);

      //3.封装pageInfo
        return new PageInfo<>(list);
    }

    @Override
    public void addCategoryBrand(CategoryBrand categoryBrand) {
        //1.判断关系数据是否存在
         Integer count =  categoryBrandMapper.selectByCtaegoryIdAndBrandId(categoryBrand);
         if (count>0){
             throw  new GuiguException(ResultCodeEnum.CATEGORY_BRAND_ERROR);
         }

         //2.建议关系数据
        categoryBrandMapper.addCategotyBrand(categoryBrand);
    }

    @Override
    public List<Long> getIdByCategory3Id(Long category3Id) {
        //根据category3Id查询当前分类对象
        //查询2级
        Category category2 = categoryMapper.selectById(category3Id);
        //查询1级
        Category category1 = categoryMapper.selectById(category2.getParentId());

        List<Long> idList = new ArrayList<>();
        idList.add(category1.getParentId());
        idList.add(category2.getParentId());
        idList.add(category3Id);

        return idList;

    }

    @Override
    public void deletedById(Integer id) {
        categoryBrandMapper.deletedById(id);
    }

    @Override
    public void updateCategoryBrand(CategoryBrand categoryBrand) {
     categoryBrandMapper.updateCategoryBrand(categoryBrand);

    }
}
